/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package org.amarena2d.examples.scenes;

import com.android1.amarena2d.nodes.FpsLabel;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.nodes.SimpleLabel;
import com.android1.amarena2d.nodes.particle.Emitter;
import com.android1.amarena2d.nodes.particle.ParticleSystem;
import com.android1.amarena2d.nodes.particle.effects.*;
import com.android1.amarena2d.texture.Textures;
import org.amarena2d.examples.Example;

import java.util.ArrayList;

@Example(name = "Default Particle Effects", category = "Particle System")
public class ParticleSystemExampleScene extends Scene {

    int selected = 0;
    ArrayList<Item> emitter = new ArrayList<Item>(20);

    SimpleLabel label;

    public ParticleSystemExampleScene() {

        final float x = engine.getCamera().getX();
        final float y = engine.getCamera().getY();

        //place the default FPS counter into the screen
        add(new FpsLabel(10, 20));

        ParticleSystem system = new ParticleSystem(Textures.$("particle.png").get());

        ExplosionEffect explosionEffect = new ExplosionEffect();
        float hsize = explosionEffect.getEffectConfig().startSize / 2;
        emitter.add(new Item("ExplosionEffect", system.createEmitter(explosionEffect, x - hsize, y - hsize).setDoLoop(true)));

        RadialTestEffect radialTestEffect = new RadialTestEffect();
        hsize = radialTestEffect.getEffectConfig().calcCenter();
        emitter.add(new Item("RadialTestEffect", system.createEmitter(radialTestEffect, x - hsize, y - hsize)));

        TrippEffect trippEffect = new TrippEffect();
        hsize = trippEffect.getEffectConfig().calcCenter();
        emitter.add(new Item("TrippEffect", system.createEmitter(trippEffect, x - hsize, y - hsize)));

        StarfieldEffect starfieldEffect = new StarfieldEffect();
        hsize = starfieldEffect.getEffectConfig().calcCenter();
        emitter.add(new Item("StarfieldEffect", system.createEmitter(starfieldEffect, x - hsize, y - hsize)));

        FireEffect fireEffect = new FireEffect();
        hsize = fireEffect.getEffectConfig().calcCenter();
        emitter.add(new Item("FireEffect", system.createEmitter(fireEffect, x - hsize, y - hsize)));

        SnowEffect snowEffect = new SnowEffect();
        hsize = snowEffect.getEffectConfig().calcCenter();
        emitter.add(new Item("SnowEffect", system.createEmitter(snowEffect, x - hsize, engine.getCamera().getHeight())));

        SmokeEffect smokeEffect = new SmokeEffect();
        hsize = smokeEffect.getEffectConfig().calcCenter();
        emitter.add(new Item("SmokeEffect", system.createEmitter(smokeEffect, x - hsize, -50)));

        SunEffect sunEffect = new SunEffect();
        hsize = sunEffect.getEffectConfig().calcCenter();
        emitter.add(new Item("SunEffect", system.createEmitter(sunEffect, x - hsize, y - hsize)));

        GalaxyEffect galaxyEffect = new GalaxyEffect();
        hsize = galaxyEffect.getEffectConfig().calcCenter();
        emitter.add(new Item("GalaxyEffect", system.createEmitter(galaxyEffect, x - hsize, y - hsize)));

        MeteorEffect meteorEffect = new MeteorEffect();
        hsize = meteorEffect.getEffectConfig().calcCenter();
        emitter.add(new Item("MeteorEffect", system.createEmitter(meteorEffect, x - hsize, y - hsize)));

        add(system);


        add(label = new SimpleLabel(emitter.get(0).name + "(Touch screen for next effect)", 100, 20));

    }

    @Override
    protected void onEnterFinished() {
        touch().makeTouchable(this);
        emitter.get(0).emitter.play();
    }

    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
        next();
        return true;
    }

    private void next() {
        Emitter emitter = this.emitter.get(selected).emitter;
        emitter.stop();
        emitter.reset();
        selected++;
        if (selected == this.emitter.size()) {
            selected = 0;
        }
        label.setText(this.emitter.get(selected).name);
        this.emitter.get(selected).emitter.play();

    }

    private final class Item {
        public String name;
        public Emitter emitter;

        private Item(String name, Emitter emitter) {
            this.name = name;
            this.emitter = emitter;
        }
    }
}